/**
 * Definition for a binary tree node.

 */
#include<vector>
#include<stack>
using std::stack;
using std::vector;

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution
{
public:
    vector<int> inorderTraversal(TreeNode *root)
    {
        if (root == nullptr) return {};
        vector<int> result;
        stack<TreeNode*> s;
        TreeNode *x = root;
        while (true) {
            while (x) {
                s.push(x);
                x = x->left;
            }
            if (s.empty()) break;
            x = s.top();
            s.pop();
            result.push_back(x->val);
            x = x->right;
        }
        return result;
        
    }

};